package com.bw.user.mvvm.viewmodel;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_common.dao.UserBean;
import com.bw.mvvm_common.dao.UserBeanDao;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;
import com.bw.mvvm_common.mvvm.view.BaseFragment;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.user.BR;
import com.bw.user.R;
import com.bw.user.mvvm.model.UserModel;
import com.bw.user.mvvm.view.fragment.DeletActivity;
import com.bw.user.mvvm.view.fragment.MyOrderActivity;
import com.bw.user.mvvm.view.fragment.UserDetailsActivity;
import com.bw.user.mvvm.view.fragment.UserLoginActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.List;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/19 0019 上午 10:27
 *
 */
class UserViewmodel  extends BaseViewModel<UserModel>{
    public ObservableField<String> username = new ObservableField<>();
    List<UserBean> list;

    @Override
    public void onCreate() {
        super.onCreate();
//        EventBus.getDefault().register(this);
        username.set("登录/注册");

        resume();


    }


    public void resume(){
        UserBeanDao green = GreenDaoUtils.getGreenDaoUtils().green();
        list = green.queryBuilder().list();
        if (list.isEmpty()){
            ToastUtils.showLong("未登录");
            username.set("登录/注册");
        }
        else {
            username.set(list.get(0).getUser_mobile());
        }
    }


//    @Subscribe(sticky = true)
//    public void username(String name){
//
//        username.set(name);
//
//    }


    public void loginbtn(){
        if (!list.isEmpty()){
            ToastUtils.showLong("已登录");
            HashMap<String, Object> map = new HashMap<>();
            map.put(UIField.PAGECLAZZ, UserDetailsActivity.class);
            startPage(map);
        }else {
            HashMap<String, Object> map = new HashMap<>();
            map.put(UIField.PAGECLAZZ, UserLoginActivity.class);
            startPage(map);
        }


    }


    public void delet(){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put(UIField.PAGECLAZZ, DeletActivity.class);
        startPage(hashMap);
    }

    @Override
    protected void result(BaseEntity entity) {

    }

    public void orderactivity(){
        HashMap<String,Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, MyOrderActivity.class);
        startPage(map);
    }
    @Override
    public void onDestory() {
        super.onDestory();
//        EventBus.getDefault().unregister(this);
    }
}
